package com.lemon.smartsafetynew.ui.fragment.message;

import android.os.Bundle;

import com.google.gson.Gson;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.SharedPreferencesServer;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenDtoResult;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingCheckItemVo;
import com.lemon.smartsafetynew.entity.message.MessageVo;
import com.lemon.smartsafetynew.net.services.PushMessageServices;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenInfoActivity;
import com.lemon.smartsafetynew.ui.activity.ledger.LedgerBuildingCheckItemDetailActivity;
import com.lemon.smartsafetynew.ui.adapter.message.MessageListAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * 推送消息
 * Created by Andrew on 2018/4/19.
 */
public class MessageListFragment extends BasePullToRefreshFragment<MessageVo, List<MessageVo>>
        implements RecyclerTClickListener<MessageVo> {

    private String status = "";   // "0" 已读  "1" 未读
    private Gson gson;

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_PUSH_MESSAGE_LIST_STATUS)) {
                status = bundle.getString(KeyConstants.KEY_PUSH_MESSAGE_LIST_STATUS, "");
            }
        }
    }

    public static MessageListFragment newInstance(String status) {

        Bundle args = new Bundle();

        args.putString(KeyConstants.KEY_PUSH_MESSAGE_LIST_STATUS, status);

        MessageListFragment fragment = new MessageListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onPullRefresh() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("user_id", AccountUtil.getUserID());
        params.put("is_read", status);
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        PushMessageServices.init().getNoticeList(RefreshType.REFRESH, params, status);

    }

    @Override
    public void onLoadMore() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("user_id", AccountUtil.getUserID());
        params.put("is_read", status);
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        PushMessageServices.init().getNoticeList(RefreshType.LOAD_MORE, params, status);

    }

    @Override
    public boolean goRefreshOnCreate() {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        doPullRefresh();
    }

    @Override
    public LemonRcvAdapter<MessageVo> getLemonAdapter() {
        return new MessageListAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_PUSH_MESSAGE_LIST + status;
    }

    @Override
    public void onItemClick(MessageVo data, int position) {
        Bundle bundle = new Bundle();
        // 跳转隐患
        LemonRequestParams params = new LemonRequestParams("");
        params.put("user_id", AccountUtil.getUserID());
        params.put("msg_id", data.getMsg_id());
        PushMessageServices.init().readMessage(params);
        int formType = data.getInform_type();
        switch (formType) {
            case 1:  //隐患通知
                getHiddenDangerData(data);
                break;
            case 2:  //到期提醒
                break;
            case 3:  //巡查通知
                LedgerBuildingCheckItemVo ledgerBuildingCheckItemVo = new LedgerBuildingCheckItemVo();
                ledgerBuildingCheckItemVo.setItem_id(data.getMsg_flag());
                ledgerBuildingCheckItemVo.setItem_name(data.getItemName());
                bundle.putSerializable(KeyConstants.KEY_LEDGER_PATROL_CHECK_ITEM_VO, ledgerBuildingCheckItemVo);
                UIHelper.showActivity(getmContext(), bundle, LedgerBuildingCheckItemDetailActivity.class);
                break;
            case 4:  //预警报警 {电气火灾，高危工艺，储罐监测，可燃/有毒气体}
                //TODO
                break;
        }
    }


    /**
     * 获取隐患信息，并跳转隐患详情
     */
    public void getHiddenDangerData(MessageVo data) {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("danger_id", data.getMsg_flag());

        SharedPreferencesServer mSharedPreferencesServer = new SharedPreferencesServer(getmContext());
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_SAFE_HIDDEN, Constants.SERVER_V2);
        HttpOkBase.httpBasePost(params, serverUrl, "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {
                mHUDUtil.hidden();

                if (httpClient) {
                    //成功
                    gson = new Gson();
                    SafeHiddenDtoResult dtoResult = gson.fromJson(result, SafeHiddenDtoResult.class);

                    Bundle bundle = new Bundle();
                    bundle.putSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO, dtoResult.getResult());
                    UIHelper.showActivity(getmContext(), bundle, SafeHiddenInfoActivity.class);
                } else {
                    mHUDUtil.showErrorTip(result, new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });
                }
            }
        });
    }
}
